package com.solvePlat.service.rabbitmqService;

import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import com.solvePlat.dao.UserMapper;
import com.solvePlat.entity.ResourceInfoUser;
import com.solvePlat.entity.ResourceInfoUserExtend;
import com.solvePlat.entity.User;
import com.solvePlat.service.Impl.UserServiceImpl;
import com.solvePlat.shiro.JWTToken;
import org.apache.shiro.SecurityUtils;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

@Component
public class ResourceDirectSolveService {

    @Resource
    private UserServiceImpl userServiceImpl;

    @Resource
    protected UserMapper userMapper;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "direct_resource", durable = "true"),
            exchange = @Exchange(value = "direct", type = ExchangeTypes.DIRECT),
            key = "user1")
    )
    public void process(@Payload String data, Channel channel, @Headers Map<String, Object> headers) throws Exception{

        ResourceInfoUserExtend resourceInfoUser = JSONObject.parseObject(data,ResourceInfoUserExtend.class);
        //得到需要消耗多少积分,得到当前用户
        User user  = userServiceImpl.getUserIntegral(resourceInfoUser.getCurrentUser().getUuid());
        if (user.getIntegral().intValue() >= resourceInfoUser.getResourceInfoUser().getCostGrade()){
            //扣除当前人的资源分
            userServiceImpl.reduceUserIntegral(resourceInfoUser.getResourceInfoUser().getCostGrade(),user.getUuid());
            //增加当前人的资源分
            userServiceImpl.addUserIntegral(resourceInfoUser.getResourceInfoUser().getCostGrade(),resourceInfoUser.getResourceInfoUser().getUserId());
        }
    }
}
